import Vue from "vue";

import Vuex from 'vuex';

Vue.use(Vuex);

const state = {
  count: 1,
}
const mutations = {
  add(state, n) {
    state.count += n;
  },
  substract(state, n) {
    state.count -= n;
  }
}
// 类似过滤器，每次state取值都会执行它，类似安检，和模板的computed属性比较像
const getters = {
  // 里面是一个个的方法，每个方法里面的参数是一个state
  count(state) {
    return state.count += 100;
  }
}
const actions = {
  addAction(context) {
    context.commit('add', 5);
    setTimeout(() => {
      context.commit('substract', 1);
    },3000);
    console.log('我是异步先执行的');
  },
  substractAction({ commit }) {
    commit('substract', 10);
  }
}

const moduleA = {
  state, mutations,getters,actions,
}
const moduleB = {
  state, mutations,getters,actions,
}
export default new Vuex.Store({
  // state,
  // mutations,
  // getters,
  // actions,
  modules: {
    a: moduleA,
    b: moduleB,
  }
})